package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author liuyaning
 * @version 1.0
 * @date 2021/7/30 13:49
 */
@RestControllerAdvice //控制器增强
public class ExceptionCatch  {

    @ExceptionHandler(Exception.class)//捕获exception类异常
    public ResponseResult exception(Exception e){
        e.printStackTrace();//必须打印异常栈信息，否则出异常但是控制台没提示
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }


    @ExceptionHandler(CustomException.class)//捕获CustomException类异常
    public ResponseResult exception(CustomException ce){
        System.out.println(ce);
        return ResponseResult.errorResult(ce.getAppHttpCodeEnum());
    }
}
